//
//  FireBallEnemy.m
//  iFly
//
//  Created by User-23 on 12/13/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "FireBallEnemy.h"
#import "SoundHandler.h"
#import "Constants.h"

@implementation FireBallEnemy

+(instancetype)fireBallWithPosition:(CGPoint)position andHealth:(NSInteger)health andIsCharged:(BOOL)charged {
    NSString* ballName = @"ball";
    
    if(charged){
        ballName = @"FireBall";
        [[SoundHandler sharedSoundPlayer] playSoundWithName:@"chargedFireball" andType:@"wav"];
    }
    else {
        [[SoundHandler sharedSoundPlayer] playSoundWithName:@"fireball" andType:@"mp3"];
    }
    
    FireBallEnemy* fireBall = [super enemyWithImageNamed:[NSString stringWithFormat:@"%@01",ballName] andPosition:position andHealth:health andPointsForKill:0];
    
    NSMutableArray* textures = [[NSMutableArray alloc]init];
    
    for (int idx=1; idx <= 2; idx++) {
        NSString* currentTextureName = [NSString stringWithFormat:@"%@0%d",ballName,idx];
        SKTexture* texture = [SKTexture textureWithImageNamed:currentTextureName];
        [textures addObject:texture];
    }
    SKAction* shuffle = [SKAction animateWithTextures:textures timePerFrame:0.05];
    SKAction* repeat = [SKAction repeatActionForever:shuffle];
    [fireBall runAction:repeat];
    
    return fireBall;
}

-(void)moveToPosition:(CGPoint)position {
    // Shooting the missile at the ship
    CGPoint location = position;
    // Determine offset of location to projectile
    CGPoint offset = [VectorHelper vectorSubPoint:location toPoint:self.position];
    
    // Get the direction of where to shoot
    CGPoint direction = [VectorHelper vectorNormalize:offset];
    
    // Make it shoot far enough to be guaranteed off screen
    CGPoint shootAmount = [VectorHelper vectorMultPoint:direction withMultiplier:outOfScreenOffset];
    
    // Add the shoot amount to the current position
    CGPoint realDest = [VectorHelper vectorAddPoint:shootAmount toPoint:self.position];
    
    self.zRotation = -direction.y;
    
    SKAction* move = [SKAction moveTo:realDest duration:5];
    SKAction* moveDone = [SKAction removeFromParent];
    [self runAction:[SKAction sequence:@[move, moveDone]]];
}

@end
